DIP-Python tutorials for image processing and machine learning(55-58)-Supervised Learning

学习自 Youtube 博主 DigitalSreeni。

正文

55 - How to read proprietary microscope images into Python

(给使用显微镜的人看的,略)

56 - What are features in machine learning

(听不懂,寄)

png

57 - How to generate features in Python for machine learning

python
import matplotlib.pyplot as plt
import cv2
 
img = cv2.imread('images/scratch.jpg', 0)
plt.imshow(img, cmap='gray')
<matplotlib.image.AxesImage at 0x181098b9220>
png
  • 这个图像很难使用传统的直方图方法来分割

  • 使用熵过滤器来分割,像素点越白,表示熵越高

python
from skimage.filters.rank import entropy
from skimage.morphology import disk
 
entropy_img = entropy(img, disk(1))
plt.imshow(entropy_img, cmap='gray')
<matplotlib.image.AxesImage at 0x1810981b2e0>
png
  • 但在如下图像中,熵过滤器的效果并不好
python
import matplotlib.pyplot as plt
import cv2
from skimage.filters.rank import entropy
from skimage.morphology import disk
 
img = cv2.imread('images/Yeast_Cells.png', 0)
entropy_img = entropy(img, disk(1))
 
fig = plt.figure(figsize=(10, 10))
 
ax1 = fig.add_subplot(121)
ax1.imshow(img, cmap='gray')
ax1.title.set_text('img')
 
ax2 = fig.add_subplot(122)
ax2.imshow(entropy_img, cmap='gray')
ax2.title.set_text('entropy_img')
plt.show()
png
  • 使用 Sobel 过滤器
python
import matplotlib.pyplot as plt
import cv2
from skimage.filters.rank import entropy
from skimage.morphology import disk
 
img = cv2.imread('images/Yeast_Cells.png', 0)
entropy_img = entropy(img, disk(1))
 
from scipy import ndimage as nd
 
gaussian_img = nd.gaussian_filter(img, sigma=3)
 
from skimage.filters import sobel
 
sobel_img = sobel(img)
 
fig = plt.figure(figsize=(10, 10))
 
ax1 = fig.add_subplot(131)
ax1.imshow(img, cmap='gray')
ax1.title.set_text('img')
 
ax2 = fig.add_subplot(132)
ax2.imshow(gaussian_img, cmap='gray')
ax2.title.set_text('gaussian_img')
 
ax3 = fig.add_subplot(133)
ax3.imshow(sobel_img, cmap='gray')
ax3.title.set_text('sobel_img')
 
plt.show()
png
  • 使用 Pandas 统计数据用于机器学习
python
import matplotlib.pyplot as plt
import cv2
from skimage.filters.rank import entropy
from skimage.morphology import disk
from scipy import ndimage as nd
from skimage.filters import sobel
import pandas as pd
 
img = cv2.imread('images/Yeast_Cells.png', 0)
img2 = img.reshape(-1)
 
df = pd.DataFrame()
df['Original Pixel Values'] = img2
 
entropy_img = entropy(img, disk(1))
entropy1 = entropy_img.reshape(-1)
df['Entropy'] = entropy1
 
gaussian_img = nd.gaussian_filter(img, sigma=3)
gaussian1 = gaussian_img.reshape(-1)
df['Gaussian'] = gaussian1
 
sobel_img = sobel(img)
sobel1 = sobel_img.reshape(-1)
df['Sobel'] = sobel1
 
df
Original Pixel ValuesEntropyGaussianSobel
01231.5849631160.027311
11221.5000001130.025565
21162.0000001090.032590
31142.0000001030.063612
4992.000000970.098479
...............
10485711112.0000001070.022570
10485721182.0000001070.007466
10485731062.0000001080.021702
10485741091.5000001080.012783
10485751121.5849631080.005280

1048576 rows × 4 columns

58 - What are Gabor filters

g(x,y;λ,θ,ψ,σ,γ)=exp(x2+γ2y22σ2)exp[i(2πxλ+ψ)]g(x,y;\lambda,\theta,\psi,\sigma,\gamma)=\exp \left(-\frac{x'^2+\gamma^2y'^2}{2\sigma^2}\right)\cdot\exp\left[i\left(2\pi\frac{x'}{\lambda}+\psi\right)\right]

  • 根据欧拉公式:

eix=cosx+isinxe^{ix}=\cos x + i \sin x

  • 实数部分:

g(x,y;λ,θ,ψ,σ,γ)=exp(x2+γ2y22σ2)cos(2πxλ+ψ)g(x,y;\lambda,\theta,\psi,\sigma,\gamma)=\exp \left(-\frac{x'^2+\gamma^2y'^2}{2\sigma^2}\right)\cdot\cos\left(2\pi\frac{x'}{\lambda}+\psi\right)

  • 虚数部分:

g(x,y;λ,θ,ψ,σ,γ)=exp(x2+γ2y22σ2)sin(2πxλ+ψ)g(x,y;\lambda,\theta,\psi,\sigma,\gamma)=\exp \left(-\frac{x'^2+\gamma^2y'^2}{2\sigma^2}\right)\cdot\sin\left(2\pi\frac{x'}{\lambda}+\psi\right)

其中 x=xcosθ+ysinθx'=x\cos\theta + y\sin \theta, y=xsinθ+ycosθy'=-x\sin\theta + y\cos \theta

  • λ\lambda represents the wavelength of the sinusoidal factor.

    • λ\lambda表示正弦因子的波长。
  • θ\theta represents the orientation of the normal to the parallel stripes of a Gabor function.

    • θ\theta表示Gabor函数平行条纹的法线方向。
  • ψ\psi is the phase offset.

    • ψ\psi 为相位偏移量。
  • σ\sigma is the sigma/standard deviation of the Gaussian envelope.

    • σ\sigma 为高斯包络线的标准差/标准差。
  • γ\gamma is the spatial aspect ratio, and specifies the ellipticity of the support of the Gabor function.

    • γ\gamma 为空间纵横比,表示Gabor函数支持度的椭圆度。越接近 1,越像圆;越接近 0,越像椭圆。
python
# kernel = cv2.getGaborKernel((ksize, ksize), sigma, theta, lamda, gamma, phi, ktype=cv2.CV_32F)
python
import numpy as np
import cv2
import matplotlib.pyplot as plt
 
ksize = 5
sigma = 3
theta = 1 * np.pi / 4
lamda = 1 * np.pi / 4
gamma = 0.5
phi = 0
 
kernel = cv2.getGaborKernel((ksize, ksize), sigma, theta, lamda, gamma, phi, ktype=cv2.CV_32F)
plt.imshow(kernel, cmap='gray')
<matplotlib.image.AxesImage at 0x2021c8e8d90>
png
python
img = cv2.imread('images/synthetic.jpg', 0)
plt.imshow(img, cmap='gray')
<matplotlib.image.AxesImage at 0x2021c966fd0>
png
python
fimg = cv2.filter2D(img, cv2.CV_8UC3, kernel)
plt.imshow(fimg, cmap='gray')
<matplotlib.image.AxesImage at 0x2021c9bec40>
png

通过修改 θ\theta 的值来使 Gabor 过滤器识别右下斜线。

python
import numpy as np
import cv2
import matplotlib.pyplot as plt
 
ksize = 5
sigma = 3
theta = 3 * np.pi / 4
lamda = 1 * np.pi / 4
gamma = 0.5
phi = 0
 
kernel = cv2.getGaborKernel((ksize, ksize), sigma, theta, lamda, gamma, phi, ktype=cv2.CV_32F)
fimg = cv2.filter2D(img, cv2.CV_8UC3, kernel)
plt.imshow(fimg, cmap='gray')
<matplotlib.image.AxesImage at 0x2021c697b20>
png